package com.tchintech.pay.view.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.tchintech.pay.R;
import com.tchintech.pay.adapter.CardListAdapter;
import com.tchintech.pay.delegate.CardDelegate;
import com.tchintech.pay.frame.presenter.ActivityPresenter;
import com.tchintech.pay.model.entity.Card;
import com.tchintech.pay.model.entity.HttpCode;
import com.tchintech.pay.net.OkhttpClientTest;
import com.tchintech.pay.utils.AppConst;
import com.tchintech.pay.utils.TimeUtil;
import com.tchintech.pay.widget.DialogWiget;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/10 0010.
 */

public class CardActivity extends ActivityPresenter<CardDelegate> implements View.OnClickListener {
    private ListView listView;
    private List<Card> list;
    private CardListAdapter cardListAdapter;
    private String card_url = "BackCard.query";
    private Map<String, String> map;
    private Handler handler;
    private String state = "";
    private String channel_name = "";

    private DialogWiget dialog = new DialogWiget();
    @Override
    protected void initView() {
        super.initView();
        listView = viewDelegate.getLv();
        viewDelegate.setTv_title("银行卡管理");
        viewDelegate.setVisible();
        viewDelegate.setOnClickListener(this, R.id.iv_back, R.id.iv_add);
        cardListAdapter = new CardListAdapter(this, list);
        listView.setAdapter(cardListAdapter);
        handler();
        queryData();
        if (state.equals("2")) {
            //付款界面进入，需注册item的点击事件
            setListener();
        }

    }

    private void setListener() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!TimeUtil.isDeal()) {
                    dialog.showMessage(CardActivity.this, "无法交易", "交易时段为每天08:00~22:00");
                    return;
                }
                Card card = (Card) parent.getItemAtPosition(position);
                Intent intent = new Intent();
                intent.putExtra("card_num", card.getBank_number());
                //请求代码可以自己设置，这里设置成20
                setResult(RESULT_OK, intent);
                //关闭掉这个Activity
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        list = new ArrayList<Card>();
        state = getIntent().getStringExtra("state");
        channel_name = getIntent().getStringExtra("channel_name");
    }

    private void handler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                hideProgressDialog();
                switch (msg.what) {
                    case HttpCode.networkabnormal:
                        showMsg("当前网络不可用");
                        break;
                    case HttpCode.http_failed:
                        showMsg("连接失败");
                        break;
                    case HttpCode.http_timeout:
                        showMsg("请求超时");
                        break;
                    case HttpCode.CARD_MANAGER:
                        try {
                            JSONObject json = new JSONObject(msg.obj.toString());
                            if (json.getString("ret").equals("1")) {
                                setData(json.getString("data"));
                            } else {
                                showMsg("查询失败");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            showMsg("查询失败");
                        }

                        break;
                }
            }
        };
    }


    /**
     * 绑定数据
     */
    private void setData(String json) {
        try {
         /*   JSONObject jsonObject = new JSONObject(json);
            JSONArray jsonArray = jsonObject.getJSONArray("msg");*/

            JSONArray jsonArray = new JSONArray(json);
            if (jsonArray.length() < 1 && state.equals("2")) {
                showMsg("请绑定信用卡");
                return;
            }
            for (int i = 0; i < jsonArray.length(); i++) {
                Card card = getGson().fromJson(jsonArray.getJSONObject(i).toString(), Card.class);
                list.add(card);
            }
            cardListAdapter.setList(list);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 查询数据
     */
    private void queryData() {
        map = new HashMap<String, String>();
        map.put("id", AppConst.getUser().getId());
        map.put("channel_name", channel_name);
        showProgressDialog("", "加载中，请稍后...");
        new OkhttpClientTest().path(this, handler, card_url, map, HttpCode.CARD_MANAGER);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_add:
                Intent intent = new Intent();
                intent.setClass(this, BoundcaedActivity.class);
                intent.putExtra("id", AppConst.getUser().getId());
                intent.putExtra("isfirst", false);
                intent.putExtra("bound_enter", "1");
                startActivityForResult(intent, 1);

                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    list.clear();
                    cardListAdapter.setList(list);
                    queryData();
                }
                break;

        }
    }

    @Override
    protected Class<CardDelegate> getDelegateClass() {
        return CardDelegate.class;
    }
}
